#ifndef ZS_PLAYER_MGR_H
#define ZS_PLAYER_MGR_H

#include <nel/net/login_server.h>
#include <nel/misc/time_nl.h>
#include <nel/misc/singleton.h>
#include <game_share/object_pool.h>

#include "player.h"

namespace ZS
{
class CPlayerMgr : public NLMISC::CSingleton<CPlayerMgr>
{
public:

	void init( uint32 playerMax )
	{
		_playerMax = playerMax;
		_playerPool.init( "ZS Player Pool", _playerMax );
	//	_playerMap.resize(_playerMax*5);
	}

	CPlayer* alloc()
	{
		return new CPlayer;
	}

	void free( CPlayer* pPlayer )
	{
		if (pPlayer != NULL)
		{
			delete pPlayer;
		}
	}

	bool push( CPlayer* pPlayer )
	{
		if( pPlayer == NULL )
		{
			return false;
		}

		_PLAYER_MAP::iterator iter = _playerMap.find( pPlayer->getID() );

		if(iter == _playerMap.end())
		{
			_playerMap.insert( std::make_pair(pPlayer->getID(), pPlayer) );
			return true;
		}

		nlinfo("Warning**  EGS::CPlayerMgr Memory leak. playerID:%"NL_I64"d",pPlayer->getID());
		free(pPlayer);
		return false;
	}

	CPlayer* findPlayer(DEF::UID id)
	{
		CPlayer* pPlayer = NULL;

		_PLAYER_MAP::iterator iter = _playerMap.find(id);
		if( iter != _playerMap.end() )
		{
			pPlayer = iter->second;
		}

		return pPlayer;
	}

	void removePlayer(DEF::UID id)
	{
		_PLAYER_MAP::iterator iter = _playerMap.find(id);
		if( iter != _playerMap.end() )
		{
			free(iter->second);
			_playerMap.erase(iter);
		}
	}


private:

	typedef CHashMap<DEF::UID, CPlayer*>		_PLAYER_MAP;

	uint32        _playerMax;
	_PLAYER_MAP   _playerMap;
	
	NLMISC::CObjectPool<CPlayer>    _playerPool;
};

#define  PlayerMgr  CPlayerMgr::instance()

}
#endif		//	ZS_PLAYER_MGR_H









